package com.jingdianjichi.wx.handler;

import com.jingdianjichi.wx.enums.MsgTypeEnum;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class WxMsgFactory implements InitializingBean {

    @Resource
    private List<MsgTypeHandler> msgTypeHandlerList;

    private Map<MsgTypeEnum,MsgTypeHandler> map = new HashMap<>();
    public MsgTypeHandler getHandler(String msgType){
        return map.get(MsgTypeEnum.getByMsgType(msgType));
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        for (MsgTypeHandler msgTypeHandler : msgTypeHandlerList) {
            map.put(msgTypeHandler.getByMsgType(), msgTypeHandler);
        }
    }
}
